#creates all of the functions that are used by the RunParser file

class Level2Loader(object):
#load the lines from the file

    def __init__(self):
        self.lines = []

    def load_from_file(self, filepath):
        # load a file into a list
        self.lines = []
        for line in open(filepath):
            self.lines.append(line)

    def load_lines(self, linesToLoad):
        # load a file into a list
        self.lines = linesToLoad

    def load_from_database(self):
        pass

class Level2Parser(object):
    def __init__(self):
        # initialize the elements
        self.load_elements()
        self.data = {}
        self.lines = []

    def load_from_file(self, filepath):
        # load a file into a list
        self.lines = []
        for line in open(filepath):
            self.lines.append(line)

    def intSplitter(self, line, matchIndex):
        #splits the line and returns the last index value as an integer
        return int(line.split()[-1])

    def load_elements(self):
        self.elements = [
            # columnname, matching string, extraction code
            ['feeling_dizzy', '15a Feeling Dizzy:', self.intSplitter],
            ['loss_balance', '15b Loss of Balance:', self.intSplitter],
            ['poor_coordination', '15c Poor coordination', self.intSplitter],
            ['headaches', '15d Headaches:', self.intSplitter],
            ['nausea', '15e Nausea:', self.intSplitter],
            ['vision_problems', '15f Vision Problems', self.intSplitter],
            ['light_sensitivity', '15g Sensitivity to light:', self.intSplitter],
            ['hearing_difficulty', '15h Hearing Difficulty:', self.intSplitter],
            ['noise_sensitivity', '15i Sensitivity to noise:', self.intSplitter],
            ['numbness', '15j Numbness or tingling on parts of my body:', self.intSplitter],
            ['taste_changes', '15k Change in taste or smell:', self.intSplitter],
            ['appetite', '15l Loss of appetite or increased appetite:', self.intSplitter],
            ['concentration', '15m Poor concentration, cannot pay attention:', self.intSplitter],
            ['forgetfulness', '15n Forgetfullness, cannot remember things:', self.intSplitter],
            ['decision_difficulty', '15o Difficulty making decisions:', self.intSplitter],
            ['thinking', '15p Slowed thinking, difficulty getting organized, cannot finish things:', self.intSplitter],
            ['fatigue', '15q Fatigue, loss of energy, getting tired easily:', self.intSplitter],
            ['sleep', '15r Difficulty falling or staying asleep:', self.intSplitter],
            ['anxiety', '15s Feeling anxious or tense:', self.intSplitter],
            ['depressed', '15t Feeling depressed or sad:', self.intSplitter],
            ['irritability', '15u Irritability, easily annoyed:', self.intSplitter],
            ['frustration', '15v Poor frustration tolerance, feeling easily overwhelmed by things:', self.intSplitter],
        ]

    def condense_lines_record15(self, linesToCondense):
        newlines = []
        fullLine = []
        for line in linesToCondense:
            if line.find('15') >= 0 and len(fullLine) > 0:
                newlines.append(' '.join(fullLine))
                fullLine = []
            fullLine.append(line.strip())
        if len(fullLine) > 0:
            newlines.append(' '.join(fullLine))
        return newlines

    def parse_lines_record15(self):
        # parse the lines using the information in elements
        currentLineNumber = 0
        uncondensedRecord15Lines = []
        condensedRecord15Lines = []
        #search through entire file
        for line in self.lines:
            #finding record type 15 and condensing the lines
            if line.startswith('15 Neurobehavioral Symptoms'):
                curIndexRecord15 = currentLineNumber + 1
                curLineTextRecord15 = line
                while not curLineTextRecord15.startswith('16.'):
                    curLineTextRecord15 = self.lines[curIndexRecord15]
                    uncondensedRecord15Lines.append(curLineTextRecord15)
                    curIndexRecord15 = curIndexRecord15 + 1
                    #print curIndexRecord15

                    #if it finds line 16, stop the loop
                    if self.lines[curIndexRecord15].startswith('16.'):
                        break
                condensedRecord15Lines = self.condense_lines_record15(uncondensedRecord15Lines)
            # increment line number by 1 before continuing.  Needs to be done at the end of "for" loop.  This counter
            # will continue looping through the file to pull out records after 16.
            #currentLineNumber = currentLineNumber + 1

        #assembled and condensed all of record 15 lines only, now load into a dictionary
        for line in condensedRecord15Lines:
            #print line

            for element in self.elements:
                # check if there is a match for the string in first item of
                # array
                match = line.rfind(element[1])
                # array
                match = line.rfind(element[1])
                # check for match
                if match >= 0:
                    # extract out the value
#                    print line, element[1], match
                    value = element[2](line, match)
                    self.data[element[0]] = value
